package com.zjx.springcloud.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.google.gson.Gson;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.zjx.springcloud.constant.HttpCode;
import com.zjx.springcloud.entities.Dept;
import com.zjx.springcloud.entities.ResultVO;
import com.zjx.springcloud.entities.test.Test;
import com.zjx.springcloud.service.DeptService;
import com.zjx.springcloud.utils.LanguageUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
/**
 * spring boot demo 接口
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/dept")
public class DeptController {

     @Autowired
    private DeptService deptService;

     //搜索引擎
    @Autowired
    private SolrClient solrClient;

    //微服务发现

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/queryAll")
    public ResultVO queryAll(
            Test dept
        ) {
        System.out.println("****8001");
        System.out.println("****u"+dept.getUsername());
        System.out.println("****p"+dept.getPass());
        System.out.println("****m"+dept.getMoney());
        System.out.println("****m"+dept.getLang());
         List<Dept> deptList=deptService.queryDeptAll();





        return new ResultVO(HttpCode.REQUEST_SUCCESS_CODE, LanguageUtils.getDefaultText("success"),deptList);



//
//        try {
//            System.out.println("****i");
//
//            List<Dept> deptList=deptService.queryDeptAll();
//
//            List<String> a=client.getServices();
//            for(String as:a){
//                System.out.println("****个数"+as);
//            }
//
//            return new ResultVO(HttpCode.REQUEST_SUCCESS_CODE, LanguageUtils.getDefaultText("success"),deptList);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("****e.printStackTrace()");
//            logger.error(""+e.getMessage());
//            return new ResultVO(HttpCode.REQUEST_ERROR_CODE, LanguageUtils.getDefaultText("request_error"));
//        }
    }

    @RequestMapping(value = "/add")
        public ResultVO add(
            Test dept
    )  {


        UpdateResponse response = null;
        try {

            response = solrClient.addBean(dept);
            int staus = response.getStatus();
            if (staus != 0) {
                solrClient.rollback();
            }else{
                solrClient.commit();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }





        return new ResultVO(HttpCode.REQUEST_SUCCESS_CODE, LanguageUtils.getDefaultText("success"));

    }
    @RequestMapping(value = "/query")
    public ResultVO query(
            Test dept
    ) throws IOException, SolrServerException {


        //创建solr的查询条件对象
        SolrQuery query = new SolrQuery();
        //查询所有
        query.setQuery("*:*");
        query.setSort("id", SolrQuery.ORDER.desc);
        //查询并获取响应对象
        QueryResponse queryResponse = solrClient.query(query);
        //从查询响应中获取查询结果集对象
        SolrDocumentList results = queryResponse.getResults();

        //获取查询条数
        System.out.println("查询的条数为："+results.getNumFound());


        return new ResultVO(HttpCode.REQUEST_SUCCESS_CODE, LanguageUtils.getDefaultText("success"));

    }
    @RequestMapping(value = "/queryId")
    public ResultVO queryId(
            int id
    ) throws IOException, SolrServerException {


        //创建solr的查询条件对象
        SolrQuery query = new SolrQuery();
        //查询所有
        query.setQuery("id:"+id);
        //查询并获取响应对象
        SolrDocument solrDocument = solrClient.getById(String.valueOf(id));
        Gson gson = new Gson();
        String solrString = gson.toJson(solrDocument);
        System.out.println("solrString："+solrString);


        return new ResultVO(HttpCode.REQUEST_SUCCESS_CODE, LanguageUtils.getDefaultText("success"));

    }
}
